{$i-,s-,g+,r-}
{$ifndef modif}{$i modif.inc}{$endif}
Program MedianFiltration;
uses {$ifdef Windows}
     WinCrtx,WinDos,
     {$else}
       {$ifdef crtx}crtx,{$else}crt,{$endif}dos,
     {$endif}
     pictures,ToolsB;

var vystup,a:string;
    fbr,obr:Picture;
    i,j:integer;
    MaxP,p:Byte;
    x,y:integer;
    MarkedTime:Longint;
    n:real;
    dispDefined:boolean;
    PassLProc:PercentLineProc;

Function GetInterval:Longint; assembler;
asm
	MOV	AH,0
        INT	1Ah
        MOV	AX,DX
        MOV	DX,CX
end;


Label 1,10,20;


const Title:array[0..17]of char='Median Filtration'#0;
begin
{$ifdef Windows}
ScreenSize.X:=62;
ScreenSize.Y:=10;
WindMax:=ScreenSize;
Move(Title,WindowTitle,Sizeof(Title));
{$endif}

PassLProc.Init;
obr.init;
fbr.init;
{$ifndef  Windows}
  DirectVideo:=False;
  nosound;
  {$endif}
writeln(#10#13'<<< MEDIAN >>> Median picture filtration (c)1995-2009 F&TSoft+PaKra');
vystup:='';
dispDefined:=false;
n:=1;
if Paramcount=0 then
	begin
1:	write('Enter name of input picture:');readln(a);
	if a='' then goto 1;
	assignpicture(obr,a);
	write('Enter name of output picture:');readln(a);
	vystup:=a;
10:     write('Enter size of mask:');readln(a);
	val(a,n,j);
	if (j<>0)or(n<=0) then
		     begin
		     writeln('Error:Incorrect number ',a);
		     goto 10;
		     end;
	end;

for i:=1 to paramcount do
	begin
	a:=paramStr(i);
	if((a[2]='?')or(a[1]='?'))or((UpCases(a)='/HELP')or(UpCases(a)='-HELP')) then
		begin
		writeln(#10#13'MEDIAN file_in file_out [size] '#10#13+
			      'Supported file formats are BMP,OKO,PCX,RAS'#10#13);
		{$ifdef Windows}InactiveWinCrt;{$endif}
		exit;
		end;

	if a[1] in ['-','+','0'..'9'] then
		      begin
		      val(a,n,j);
		      if j<>0 then begin
				   writeln('Error:Incorrect number ',a);
				   {$ifdef Windows}InactiveWinCrt;{$endif}
				   exit;
				   end;
		      end
		else
		  begin
		  if obr.filename=nil then assignpicture(obr,a);
		  vystup:=a;
		  end;

	end;

if n<0.9 then
	begin
	writeln('Error:Incorrect size');
	{$ifdef Windows}InactiveWinCrt;{$endif}
	exit;
	end;

if obr.filename=nil then
	begin
        writeln('Error:Unknown filename');
        {$ifdef Windows}InactiveWinCrt;{$endif}
        exit;
        end;
write(#13'Loading ');ClrEol;
loadpicture(obr,'',@PassLProc);
if obr.data=nil then
	begin
	writeln(#10#13'Cannot find input picture:''',obr.filename^,'''');
	{$ifdef Windows}InactiveWinCrt;{$endif}
	exit;
	end;

markedTime:=GetInterval;
write(#13'Filtering ');ClrEol;
Median(Obr,fbr,round(n),@PassLProc);

obr.Done;
write(#13'Saving ');ClrEol;
if not(savepicture(fbr,vystup,@PassLProc)) then
	begin
	writeln(#10#13'Cannot save Output picture:''',fbr.filename^,'''');
{$ifdef Windows}InactiveWinCrt;{$endif}
	exit;
        end;
writeln(#13' OK     ');
fbr.Done;
end.
